package com.zh.service.impl;

import com.zh.dao.UserProductMapper;
import com.zh.po.UserProduct;
import com.zh.service.UserProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
public class UserProductServiceImpl implements UserProductService {
    @Autowired
    private UserProductMapper userProductMapper;
    @Override
    public int addShoppingCart(UserProduct userProduct) {
        return userProductMapper.insert(userProduct);
    }

    @Override
    public int deleteShoppingCart(Integer userId,Integer productId) {
        return userProductMapper.deleteByUserId(userId,productId);
    }

    @Override
    public UserProduct findByProductId(Integer userId,Integer productId) {
        Example example = new Example(UserProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("productId",productId);
        return userProductMapper.selectOneByExample(example);
    }

    @Override
    public int updateCart(UserProduct userProduct) {
        Integer userId=userProduct.getUserId();
        Integer productId=userProduct.getProductId();
        Integer productBuyNum=userProduct.getProductBuyNum();
        Double subtotal = userProduct.getSubtotal();
        if (userId!=null && productId!=null && productBuyNum!=null){
            return userProductMapper.updateCart(userId,productId,productBuyNum,subtotal);
        }
       return 0;
    }

    @Override
    public List<UserProduct> getAllCartByUserId(Integer userId) {
        Example example = new Example(UserProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        return userProductMapper.selectByExample(example);
    }

}
